package com.lu.wemedia.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lu.api.wemedia.IWemediaClient;
import com.lu.model.admin.dto.NewsAuthDto;
import com.lu.model.admin.dto.SensitiveDto;
import com.lu.model.admin.pojo.AdChannel;
import com.lu.model.admin.pojo.AdSensitive;
import com.lu.model.common.dtos.PageResponseResult;
import com.lu.model.common.dtos.ResponseResult;
import com.lu.model.common.enums.AppHttpCodeEnum;
import com.lu.model.wemedia.dto.ChannelDto;
import com.lu.model.wemedia.pojo.WmChannel;
import com.lu.model.wemedia.pojo.WmNews;
import com.lu.model.wemedia.pojo.WmSenSitive;
import com.lu.wemedia.service.WmChannelService;
import com.lu.wemedia.service.WmNewsService;
import com.lu.wemedia.service.WmSensitiveService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class WmSensitiveController implements IWemediaClient {


    @Autowired
    private WmSensitiveService wmSensitiveService;
    @Autowired
    private WmChannelService wmChannelService;
    @Autowired
    private WmNewsService wmNewsService;


    /**
     * 敏感词删除
     * @param id
     * @return
     */
    @DeleteMapping("/api/v1/sensitive/del/{id}")
    @Override
    public ResponseResult remove(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSenSitive wmSenSitive = wmSensitiveService.getOne(Wrappers.<WmSenSitive>lambdaQuery().eq(WmSenSitive::getId, id));
        if (wmSenSitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        wmSensitiveService.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 查询敏感词列表
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/sensitive/list")
    @Override
    public ResponseResult list(SensitiveDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        Page<WmSenSitive> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSenSitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WmSenSitive::getCreatedTime);
        page = wmSensitiveService.page(page, queryWrapper);
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());

        return result;
    }


    /**
     * 保存敏感词
     * @param adSensitive
     * @return
     */
    @PostMapping("/api/v1/sensitive/save")
    @Override
    public ResponseResult save(AdSensitive adSensitive) {
        if (adSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmSenSitive wmSenSitive = wmSensitiveService.getOne(Wrappers.<WmSenSitive>lambdaQuery().eq(WmSenSitive::getSensitives, adSensitive.getSensitives()));
        if (wmSenSitive != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        WmSenSitive sitive = new WmSenSitive();
        BeanUtils.copyProperties(adSensitive, sitive);
        sitive.setCreatedTime(new Date());
        wmSensitiveService.save(sitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 修改敏感词
     * @param adSensitive
     * @return
     */
    @PostMapping("/api/v1/sensitive/update")
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if (adSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSenSitive wmSenSitive = wmSensitiveService.getOne(Wrappers.<WmSenSitive>lambdaQuery().eq(WmSenSitive::getId, adSensitive.getId()));
        if (wmSenSitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        WmSenSitive result = new WmSenSitive();
        BeanUtils.copyProperties(adSensitive, result);
        wmSensitiveService.updateById(result);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /*===================================================频道=======================================================*/

    /**
     * 频道列表
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/channel/list")
    @Override
    public ResponseResult channelList(ChannelDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        Page<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WmChannel::getCreatedTime);
        Page<WmChannel> data = wmChannelService.page(page, queryWrapper);
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) data.getTotal());
        result.setData(data.getRecords());
        return result;
    }


    /**
     * 删除频道
     * @param id
     * @return
     */
    @GetMapping("/api/v1/channel/del/{id}")
    @Override
    public ResponseResult removeChannel(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel wmChannel = wmChannelService.getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getId, id));
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        wmChannelService.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 保存频道
     * @param adChannel
     * @return
     */
    @PostMapping("/api/v1/channel/save")
    @Override
    public ResponseResult saveChannel(AdChannel adChannel) {
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel wmChannel = wmChannelService.getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getId, adChannel.getId()));
        if (wmChannel != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        WmChannel channel = new WmChannel();
        BeanUtils.copyProperties(adChannel, channel);
        wmChannelService.save(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }


    /**
     * 修改频道
     * @param adChannel
     * @return
     */
    @PostMapping("/api/v1/channel/update")
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel wmChannel = wmChannelService.getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getId, adChannel.getId()));
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        WmChannel channel = new WmChannel();
        BeanUtils.copyProperties(adChannel, channel);
        wmChannelService.updateById(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /*===================================================文章人工审核=======================================================*/

    /**
     * 文章人工审核列表查询
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/news/list_vo")
    @Override
    public ResponseResult artificialExamine(NewsAuthDto dto) {
        return wmNewsService.artificialExamine(dto);
    }

    /**
     * 查询文章人工审核的详情
     * @param id
     * @return
     */
    @GetMapping("/api/v1/news/one_vo/{id}")
    @Override
    public ResponseResult findArticle(Integer id) {
        return wmNewsService.findArticle(id);
    }


    /**
     * 人工审核失败
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/news/auth_fail")
    @Override
    public ResponseResult examineFail(NewsAuthDto dto) {
        return wmNewsService.examineFail(dto);
    }


    /**
     * 人工审核成功
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/news/auth_pass")
    @Override
    public ResponseResult examineSuccess(NewsAuthDto dto) {
        return wmNewsService.examineSuccess(dto);
    }
}
